package net.yters.model.agents.brains;

import java.io.IOException;
import java.util.ArrayList;

import net.yters.model.ModelState;
import net.yters.model.agents.DeciderAgent;
import net.yters.model.agents.concrete.ExternalInputAgent;
import net.yters.util.Box;

public class BestFirst_ClusterRatioMetric_Brain extends BrainAgent {
	public BestFirst_ClusterRatioMetric_Brain(DeciderAgent ownerAgent) {
		super(ownerAgent);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -7413637486868561853L;

	public Box f_choice = new Box(-1.0);

	@Override
	public void execute() {
		ModelState state = f_ownerAgent.f_model;

		int[] choices = {1, 2, 3, 4};

		ArrayList<ModelState> futures = new ArrayList<ModelState>();
		for(int i = 0; i < choices.length; i++)
			try {
				futures.add(new ModelState(state));
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

		double maxMetricResult = 0.0;
		int choice = 0;
			
		// TODO make a more complex statistic, i.e. average over a number of runs
		for(ModelState future : futures) { 
			int i = futures.indexOf(future);
			int ownerAgentID = f_ownerAgent.f_model.f_agents.indexOf(f_ownerAgent);
			((ExternalInputAgent) future.f_agents.get(ownerAgentID)).f_choice.f_value = choices[i];
			future.execute();
			double result = (Double) future.f_clusterRatioMetric.f_result.f_value;
			if(maxMetricResult <= result) {
				maxMetricResult = result;
				choice = choices[i];
			}
		}
		
		f_choice.f_value = choice;
	}
}
